package org.jeik.controllers;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.jeik.utils.WebUtils;

/**
 * 
 * @author AAsha
 *
 */
public class MsgsController {
	
	/**
	 * Add Error Message to faces messages queue.
	 * -f
	 * @param msgKey
	 */
	public static void addErrorMessageKey(SystemMsgs msgKey){
		addFacesGlobalMessage(FacesMessage.SEVERITY_ERROR, msgKey);
	}
	
	/**
	 * 
	 * @param msg
	 */
	public static void addErrorMessage(String msg){
		FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
	}
	
	/**
	 * 
	 * @param msg
	 */
	public static void addInfoMessage(String msg){
		FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
	}

    /**
     * Add a Global Message to faces context, to present it in JSF pages.
     *
     * @param severity
     * @param messageKey
     */
    public static void addFacesGlobalMessage(FacesMessage.Severity severity, SystemMsgs msgKey) {
        String summary = getLocalizedMessage(msgKey);
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(severity,
                summary,
                summary));
    }
    
    /**
     * Add a Global Message to faces context, to present it in JSF pages.
     *
     * @param severity
     * @param message
     */
    public static void addFacesGlobalMessage(FacesMessage.Severity severity, String msg) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, msg, msg));
    }

    /**
     *
     * Add a Global Message to component (field), to present it in JSF pages.
     *
     * @param clientId
     * @param severity
     * @param messageKey
     */
    public static void addFacesGlobalMessage(String clientId,
            FacesMessage.Severity severity, SystemMsgs msgKey) {
        String summary = getLocalizedMessage(msgKey);
        FacesContext.getCurrentInstance().addMessage(clientId,
                new FacesMessage(severity,
                summary,
                summary));
    }

    /**
     * Get Key's localized FacesMessag, (Default locale is used)
     *
     * @param key
     * @return
     */
    public static FacesMessage getFacesMessage(FacesMessage.Severity severity,
    		SystemMsgs msgKey) {
        String message = getLocalizedMessage(msgKey);
        return new FacesMessage(severity, message, message);
    }

    /**
     * 
     * @param msgKey
     * @return String - Specified Key Message
     */
    public static String getLocalizedMessage(SystemMsgs msgKey) {
        return WebUtils.getResourceBundle().getString(msgKey.getKey());
    }
	
}
